/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

/*
 * DialogEmailAccountTester.java
 *
 * Created on Sep 11, 2011, 3:46:50 PM
 */
package group.pals.mailgate;

import group.pals.utils.sys.MessageBox;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.WindowConstants;

/**
 *
 * @author Hai Bison
 */
public class DialogWaiting extends javax.swing.JDialog {

    /** Creates new form DialogWaiting */
    public DialogWaiting(java.awt.Window owner, String title) {
        super(owner);
        setModal(false);

        initComponents();

        setLocationRelativeTo(owner);

        setTitle(title);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        progressBarMain.setIndeterminate(true);

        initActions();
    }

    private void initActions() {
        actionHalt = new ActionHalt();
        btnHalt.setAction(actionHalt);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progressBarMain = new javax.swing.JProgressBar();
        btnHalt = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnHalt.setText("jButton1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progressBarMain, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                    .addComponent(btnHalt, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progressBarMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnHalt)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
//  public static void main(String args[]) {
//    /* Set the Nimbus look and feel */
//    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//     */
//    try {
//      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//        if ("Nimbus".equals(info.getName())) {
//          javax.swing.UIManager.setLookAndFeel(info.getClassName());
//          break;
//        }
//      }
//    } catch (ClassNotFoundException ex) {
//      java.util.logging.Logger.getLogger(DialogEmailAccountTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//    } catch (InstantiationException ex) {
//      java.util.logging.Logger.getLogger(DialogEmailAccountTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//    } catch (IllegalAccessException ex) {
//      java.util.logging.Logger.getLogger(DialogEmailAccountTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//      java.util.logging.Logger.getLogger(DialogEmailAccountTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//    }
//    //</editor-fold>
//
//    /* Create and display the dialog */
//    java.awt.EventQueue.invokeLater(new Runnable() {
//
//      public void run() {
//        DialogEmailAccountTester dialog = new DialogEmailAccountTester(new javax.swing.JFrame(), true);
//        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//          @Override
//          public void windowClosing(java.awt.event.WindowEvent e) {
//            System.exit(0);
//          }
//        });
//        dialog.setVisible(true);
//      }
//    });
//  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHalt;
    private javax.swing.JProgressBar progressBarMain;
    // End of variables declaration//GEN-END:variables

    /**
     * Shows dialog and waits.
     * @param owner
     * @param title
     */
    public void doWait() {
        new Thread() {

            @Override
            public void run() {
                setModal(true);
                setVisible(true);
            }
        }.start();
    }

    /**
     * Sets termination listener.
     * @param listener termination listener, if it is <code>null</code>, default listener
     * will be used (it will halt application if user wants).
     */
    public void setTerminationListener(AbstractAction listener) {
        btnHalt.setAction(listener == null ? actionHalt : listener);
    }

    /*----------------------------------------------------------------------------
     * ActionHalt
     */
    private ActionHalt actionHalt;

    private class ActionHalt extends AbstractAction {

        public ActionHalt() {
            super("Halt");
        }

        public void actionPerformed(ActionEvent e) {
            if (MessageBox.confirmYesNo(null, null, "Are you sure you want to halt the application?", 1)) {
                System.exit(0);
            }
        }
    }
}
